package gant.ext

import org.junit.Test
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import gant.ext.AntLoggingFactory

/**
 * Created by IntelliJ IDEA.
 * User: tbarker
 * Date: 4/12/12
 * Time: 1:48 PM
 */
class AntLoggingFactoryTest {

    def factory = new AntLoggingFactory()

    @Test
    void "empty, null or root logger name returns logger with name gant"() {
        assert "gant" == factory.getLogger(null).name
        assert "gant" == factory.getLogger("").name
        assert "gant" == factory.getLogger(Logger.ROOT_LOGGER_NAME).name
    }

    @Test
    void "retrieving a logger by name returns a logger with that name"() {
        def logger = factory.getLogger("foo")
        assert "foo" == logger.name
    }

    @Test
    void "retrieving a logger by name returns the same logger each time"() {
        def logger1 = factory.getLogger("foo")
        def logger2 = factory.getLogger("foo")

        assert logger1 == logger2
    }

    @Test
    void "default logger is always the same regardless of retrieving by null, root, gant or empty string" () {
        def loggerEmpty = factory.getLogger("")
        def loggerGant = factory.getLogger("gant")
        def loggerRoot = factory.getLogger(Logger.ROOT_LOGGER_NAME)
        def loggerNull = factory.getLogger(null)

        assert (loggerGant == loggerEmpty) &&
            (loggerGant == loggerRoot) &&
            (loggerGant == loggerNull)
    }

    @Test
    void "test that we can overwrite slf4j functionality"() {
        boolean overWritten = false
        LoggerFactory.metaClass.static.getLogger = {String name ->
            overWritten = true
        }

        LoggerFactory.getLogger("foo")
        assert overWritten
    }

}
